package com.adgroup.doremon_ad;

import model.User;
import util.Config;
import util.Utility;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import database.Database;

public class MyProfileActivity extends SherlockActivity{

	TextView tvNickname, tvEmail;
	Button btnUpdateInfor, btnLogout;
	ImageView ivAvatar;
	User u;
	private ActionBar mActionBar;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	//	MenuInflater inflater = getSupportMenuInflater();
	//	inflater.inflate(R.menu.menulogin, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
		case android.R.id.title:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	LinearLayout root;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myprofile);
		
		 AdView mAdView = (AdView) this.findViewById(R.id.adView);
		    AdRequest adRequest = new AdRequest.Builder().build();
		    mAdView.loadAd(adRequest);
		    
		//action bar
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(false);
		mActionBar.setDisplayUseLogoEnabled(true);
		mActionBar.setHomeButtonEnabled(true);
		mActionBar.setTitle(getResources().getString(R.string.titlemyprofile));
		mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_actionbar));
		
		tvNickname = (TextView) findViewById(R.id.textViewProfileNickname);
		tvEmail = (TextView) findViewById(R.id.textViewProfileEmail);
		btnUpdateInfor = (Button) findViewById(R.id.buttonProfileUpdateInfor);
		btnLogout = (Button) findViewById(R.id.buttonProfileLogout);
		ivAvatar = (ImageView) findViewById(R.id.imageViewTitleMyProfile);
		root = (LinearLayout) findViewById(R.id.rootmyprofile);
		u = new Database(MyProfileActivity.this).getUser();
		//load avatar;	
	
		if(u.getLogo()!=null && u.getLogo().length()>=5){
		AQuery aq = new AQuery(MyProfileActivity.this);
		Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
		aq.id(ivAvatar).image(u.getLogo(), true,
					true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
		}
		tvNickname.setText(u.getNickName());
		tvEmail.setText(u.getEmail());
		btnUpdateInfor.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						if(u.getType().equalsIgnoreCase(Config.typeServer)){
							Intent i = new Intent(MyProfileActivity.this, UpdateInforActivity.class);
							startActivityForResult(i, Config.requestUpdateInfor);
						}
						else{
							Utility.ShowAlert(MyProfileActivity.this, getResources().getString(R.string.error),getResources().getString(R.string.alert_error_message_updateinfor));
						}
					}
				});
		btnLogout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new Database(MyProfileActivity.this).resetUser();
				SharedPreferences share = getSharedPreferences("sharefilmllk",
					    MODE_PRIVATE);
			  SharedPreferences.Editor editor = share.edit();
			
			  editor.putLong("access_expires", 0);
			  editor.putString("access", "0");
			  
			   editor.commit();
				Intent returnIntent = new Intent();
				returnIntent.putExtra("result", u.getNickName());
				returnIntent.putExtra("logout", "ok");
				
				setResult(RESULT_OK, returnIntent);
				finish();
			}
		});

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==Config.requestUpdateInfor && resultCode==RESULT_OK){//register then login
			tvNickname.setText(data.getStringExtra("nickname"));
			tvEmail.setText(data.getStringExtra("email"));
			
		}
	}
	/*private StartAppAd startAppAd = new StartAppAd(this); 
	@Override 
	public void onResume(){ 
	      super.onResume(); 
	      startAppAd.onResume(); 
	  } 
	@Override 
	  public void onPause() { 
	      super.onPause(); 
	      startAppAd.onPause(); 
	  } 
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if(keyCode==KeyEvent.KEYCODE_HOME){
			 startAppAd.showAd(); 
			    startAppAd.loadAd(); 
		}
		return super.onKeyDown(keyCode, event);
	}*/
}
